package com.xh.bussiness.permission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.permission.bean.dto.FunctionDto;
import com.xh.bussiness.permission.bean.request.FunctionRequest;
import com.xh.bussiness.permission.dao.FunctionMapper;
import com.xh.bussiness.permission.model.Function;
import com.xh.bussiness.permission.service.IFunctionService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;

import java.util.List;


@Service
public class FunctionServiceImpl extends BaseServiceImpl<FunctionMapper, Function> implements IFunctionService {

    @Override
    public XhPage queryFunctionPage(XhPage page, FunctionRequest bean) {
        QueryWrapper qw=new QueryWrapper<Function>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }


    @Override
    public List queryFunction(FunctionRequest bean) {
        QueryWrapper qw=new QueryWrapper<Function>();
        qw.select("*");
        //拼装条件
        qw=prepareParams(bean, qw);
        return this.dao.selectList(qw);
    }


    @Override
    public String addFunction(FunctionRequest bean) {
        Function function=new Function();

        function.setName(bean.getName());
        this.insert(function);

        return null;
    }


    @Override
    public String modifyFunction(FunctionRequest bean) {
        Function function=this.getById(bean.getId());
        if(function==null){
            return "当前功能id不存在";
        }

        function.setName(bean.getName());
        this.update(function);

        return null;
    }


    @Override
    public String deleteFunction(String id) {
        Function function=this.getById(id);
        if(function==null){
            return "当前功能id不存在";
        }
        function.deleteById(id);

        return null;
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(FunctionRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }
}
